<template>
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :data="data" :page.sync="page" :permission="permissionList"
      :before-open="beforeOpen" :search.sync="search" v-model="form" ref="crud" @row-update="rowUpdate"
      @row-save="rowSave" @row-del="rowDel" @search-change="searchChange" @search-reset="searchReset"
      @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange"
      @refresh-change="refreshChange" @on-load="onLoad" @tree-load="treeLoad">

      <template slot="menuLeft">
        <el-button type="primary" icon="el-icon-plus" size="small" @click="rowAdd()">新增</el-button>
      </template>
      <!-- 自定义操作列 -->
      <template #menu="{ row, index }">
        <el-button type="primary" :size="'mini'" icon="el-icon-edit" @click="rowEdit(row, index)" style="width: 23px;
        height: 22px; padding: 0;"></el-button>

        <el-button type="danger" :size="'mini'" icon="el-icon-delete" @click="rowDel(row, index)" style="width: 23px;
        height: 22px; padding: 0;"></el-button>

      </template>
      <!-- 单元格 -->
      <template slot-scope="{ row }" slot="status">
        <el-switch v-model="row.status" :active-value="0" :inactive-value="1" active-color="#409EFF"
          inactive-color="#d2d6de" active-text="正常" inactive-text="禁用" @change="changeState(row)">
        </el-switch>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>

import { getOrderRefundReasonList, delOrderRefundReasonItem, updateOrderRefundReasonItem, setStatusOrderRefundReasonItem } from "./api/orderRefundReason";
import { mapGetters } from "vuex";

export default {
  data() {
    return {


      form: {},
      query: {},
      search: {}, // 收索默认值
      loading: true,
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
      },
      selectionList: [],
      option: {
        menuType: 'button',
        height: "auto",
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: false,

        index: true,
        indexLabel: '序号',
        searchBtnText: '提交',
        searchBtnIcon: '提交',
        emptyBtnText: '重置',
        emptyBtnIcon: '重置',
        viewBtn: false,
        addBtn: false,
        addBtnText: '添加',
        addBtnIcon: 'el-icon-plus',
        editBtn: false,
        editBtnText: '',
        selection: false,
        dialogClickModal: false,
        delBtn: false,
        delBtnText: '',
        labelWidth: 110,
        searchLabelWidth: 100,
        column: [

          {
            label: "类型",
            prop: "type",
            search: true,
            type: 'select',
            dicData: [{
              label: '退款',
              value: 1
            }, {
              label: '退货退款',
              value: 2
            }, {
              label: '换货',
              value: 3
            }],
            rules: [{
              required: true,
              message: "请选择类型",
              trigger: "change"
            }],
            // hide: true, //表格是否显示该字段（该字段的列）
            // display: false //弹出的表单是否显示
          },
          {
            label: "退款原因",
            prop: "reason",
            search: true,
          },

          {
            label: "状态",
            prop: "status",
            search: true,
            type: 'select',
            dicData: [{
              label: '正常',
              value: 0
            }, {
              label: '禁用',
              value: 1
            }],
          },

        ],
      },
      data: [],
    };
  },
  computed: {
    ...mapGetters(["permission"]),

    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.user_add, true),
        viewBtn: this.vaildData(this.permission.user_view, true),
        delBtn: this.vaildData(this.permission.user_delete, true),
        editBtn: this.vaildData(this.permission.user_edit, true),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  mounted() {
  },
  methods: {
    // 改变状态
    changeState(data) {
      const params = {
        id: data.id,
        status: data.status
      };
      setStatusOrderRefundReasonItem(params).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },

    // 新增
    rowAdd(row, index) {
      this.$refs.crud.rowAdd(row, index);
    },
    rowEdit(row, index) {
      this.$refs.crud.rowEdit(row, index);
    },
    rowDel(row, index) {
      this.$refs.crud.rowDel(row, index);
    },
    rowSave(row, done, loading) {
      let param = {
        ...row
      }
      updateOrderRefundReasonItem(param).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      let param = {
        ...row
      }
      updateOrderRefundReasonItem(param).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", '提示', {
        cancelButtonText: "取消",
        confirmButtonText: "确定",
        type: "warning",
      })
        .then(() => {
          return delOrderRefundReasonItem({ id: row.id });
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },

    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      done();
    },
    searchReset() {
      this.search = {
        type: 1
      }
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, search = {}) {
      this.loading = true;
      let param = {
        page: page.currentPage,
        size: page.pageSize,
      }
      getOrderRefundReasonList({ ...param, ...search }).then((res) => {
        const resData = res.data;
        this.data = resData.list;
        this.page.total = resData.total;
        this.loading = false;
        this.selectionClear();
      });
    },
    treeLoad(tree, treeNode, resolve) {
      // var id = tree.id;
      // get(Object.assign({  parentId: tree.id })).then((response) => {
      //   resolve(response.data.data);
      // })
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
  },
};
</script>

<style>
.el-switch__label * {
  line-height: 1;
  font-size: 12px;
  display: inline-block;
}

.el-switch__label {
  position: absolute;
  display: none;
  color: #fff !important;
  font-size: 12px !important;
}

/*打开时文字位置设置*/
.el-switch__label--right {
  z-index: 1;
  right: -6px;
}

/*关闭时文字位置设置*/
.el-switch__label--left {
  z-index: 1;
  left: 22px;
}

/*显示文字*/
.el-switch__label.is-active {
  display: block;
}

/*开关宽度*/
.el-switch .el-switch__core,
.el-switch .el-switch__label {
  width: 50px !important;
}
</style>
<style lang="scss" scoped>
// ::v-deep .el-table thead th .cell {
//   border-left: none;
// }

.normal {
  color: #409EFF;
  display: inline-block;
  display: flex;
  align-items: center;

  &::before {
    content: '';
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 10px;
    /**控制形状**/
    background-color: #409EFF;
    /**控制图标颜色**/
    margin-right: 4px
      /**图标与文字距离**/
  }
}

.un-normal {
  color: #d2d6de;
  display: inline-block;

  &::before {
    content: '';
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 10px;
    /**控制形状**/
    background-color: #d2d6de;
    /**控制图标颜色**/
    margin-right: 4px
      /**图标与文字距离**/
  }
}
</style>
